﻿using System;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace TcpApplication{
    public class Client{
        public static void BuildClient(Control status,string str){
            try {
                TcpClient tcpclnt = new TcpClient();
                status.Text += "[连接中...]" + Environment.NewLine;
                tcpclnt.Connect("127.0.0.1", 8001);
                status.Text += "[已经连接上]" + Environment.NewLine;
                NetworkStream stm = tcpclnt.GetStream();
                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(str);
                status.Text += "[数据传输中...]" + Environment.NewLine;
                stm.Write(ba, 0, ba.Length);
                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);
                for (int i = 0; i < k; i++) {
                    status.Text += Convert.ToChar(bb[i]);
                }
                status.Text += Environment.NewLine;
                tcpclnt.Close();
            }
            catch (Exception e) {
                status.Text = "错误：" + e.StackTrace;
            }
        }
    }
}